/*
 * Copyright 2008 Wink Saville
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.saville.mc;

import com.saville.debug.Log;
import com.saville.msgcomp.*;

/*
 * A MsgComp with its own thread
 */
abstract public class McBase implements IMc {

    /*
     * Abstract routines for McBase
     */
    abstract public void init(String name, MsgPump msgPump, boolean doSendCmdStarted);
    abstract public void queueMsg(Msg msg);
    abstract public void processMsg(Msg msg);
    
    /*
     * Constructor
     */
    public McBase() {
        /* C A R E F U L: derived class must call init! */
        mMcMgr = McMgr.getInstance();
    }

    /*
     * Constructor
     */
    public McBase(String name, MsgPump msgPump, boolean doSendCmdStarted) {
        mMcMgr = McMgr.getInstance();
        init(name, msgPump, doSendCmdStarted);
    }

    /*
     * Constructor
     */
    public McBase(String name, MsgPump msgPump) {
        this(name, msgPump, true);
    }

    /*
     * Constructor
     */
    //public McBase(String name) {
    //    this(name, new MsgPump(Looper.myLooper()), true);
    //}

    
    /*
     * Send CMD_STARTED
     */
    public void sendCmdStarted() {
        Msg msg = getMsgForSelf(McConst.MC_GUID, McConst.MC_CMD_STARTED);
        msg.dstId = mMcId.id;
        queueMsg(msg);
    }

    /*
     * Get name
     */
    public String getName() {
        return mName;
    }

    /*
     * Lookup the named Mc.
     * 
     * @return unique id for this Mc.
     */
    public int lookup(String name) {
        return mMcMgr.lookup(name);
    }

    protected Msg getMsg() {
        Msg m = mMcMgr.getMsg();
        m.srcId = mMcId.id;
        return m;
    }

    protected Msg getMsgForSelf(int guid, int cmd) {
        return mMcMgr.getMsg(mMcId.id, mMcId.id, guid, cmd);
    }

    protected Msg getMsg(int dstId, int guid, int cmd) {
        return mMcMgr.getMsg(mMcId.id, dstId, guid, cmd);
    }

    protected void retMsg(Msg msg) {
        mMcMgr.retMsg(msg);
    }

    protected Msg dupMsg(Msg msg) {
        return mMcMgr.dupMsg(msg);
    }

    protected Msg getReplyMsg(Msg msg, int status) {
        return mMcMgr.getReplyMsg(msg, status);
    }

    protected void sendMsg(Msg msg) {
        mMcMgr.sendMsg(msg);
    }

    protected void sendReplyMsg(Msg msg, int status) {
        mMcMgr.sendReplyMsg(msg, status);
    }

    protected MsgPump mMsgPump;
    protected String mName;
    protected McId mMcId;
    protected IMcMgr mMcMgr;
    protected int df = Log.ERROR;
}
